<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
use Response;
use Crypt;
use Input;
use Log;
use DB;
use File;
use App\Models\Area;

class CodeController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
	}

	public function code()
	{
		$builder = new CaptchaBuilder();
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 34, $font = null);
        //获取验证码的内容
        $phrase = strtoupper($builder->getPhrase());

        //把内容存入session
        Session::put('code', $phrase);
        //生成图片
        return Response::make($builder->output(), 200, ['Content-Type' => 'image/jpeg']);
	}

	public function uploadImg()
	{
		$file = Input::file("imgFile");
		if(!is_null($file) && $file->isValid()){
			$ext = $file->getClientOriginalExtension();
			// if($ext != "jpg" && $ext != "jpeg" && $ext != "png"){
			// 	return self::error(self::FILE_TYPE_IS_WRONG);
			// }
			$newName = date('ymdhis') . rand(1,999) . "." . $ext;
			$path = base_path() . "/public/upload";
			$file->move($path, $newName);
			chmod($path . "/" . $newName, 0777);

			return $this->json([
				"path" =>  $newName
			]);
		}
	}

	public function addressArea()
	{

		$pid = Input::get('pid',1);
		$area = Area::where('parent_id', $pid)->get();
		return $this->json(['area'=>$area]);
	}


	public function getArea()
	{
		$pid = Input::get('pid');
		$area = Area::where('parent_id', $pid)->get();
		return $this->json(['area'=>$area]);
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
